﻿using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;


namespace EmoneyWeiXin.Controllers
{
    public class JiaWeiXinQunController : Controller
    {
        //
        // GET: /JiaWeiXinQun/
        public static object lockObj = new object();
        //  public static string imgUrl = null;
        public static string mmKey = "weixinErmkey";
        static MemcachedTools1 mmtools = new MemcachedTools1();
        public ActionResult Index()
        {
            string imgUrl = mmtools.Get<string>(StringTools.ChangeRealKey(mmKey, ""));
            if (imgUrl == null)
            {
                lock (lockObj)
                {
                    imgUrl = WeiQunTwoCodeBLL.GetUsing();
                    SetToCache(imgUrl);
                }
            }

            WebClient mywebclient = new WebClient();
            byte[] bytes = mywebclient.DownloadData(imgUrl);
            return File(bytes, "image/jpeg");
            // return File("~/Upload/" + imgUrl, "image/jpeg");
        }


        public ActionResult Main()
        {
            return View();
        }

        public ActionResult ListPage()
        {
            return View();
        }

        public ActionResult GetList()
        {
            DataTable dt = WeiQunTwoCodeBLL.GetList();
            string json = JsonTools.ToJson(dt);
            return Content(json);
        }

        public ActionResult SetPage()
        {
            return View();
        }

        public static void SetToCache(string value)
        {
            mmtools.Add(StringTools.ChangeRealKey(mmKey, ""), value, 24, 0, 0);
            //imgUrl = value;
        }

        public ActionResult QiYong()
        {
            try
            {
                string Id = Request["Id"];
                string imgUrl = Request["imgUrl"];
                WeiQunTwoCodeBLL.SetToUsing(Id);
                SetToCache(imgUrl);
                return Content("1");
            }
            catch (Exception)
            {
                return Content("0");
            }
        }


        public ActionResult ShanChu()
        {
            try
            {
                string Id = Request["Id"];
                WeiQunTwoCodeBLL.Delete(Id);
                return Content("1");
            }
            catch (Exception)
            {
                return Content("0");
            }
        }


        public ActionResult Receipt()
        {
            try
            {
                string name = Request.Form["txtQunMing"];
                HttpPostedFileBase fileBase = Request.Files[0];

                //string savePath = Server.MapPath("~/Upload");
                //string imgName = Guid.NewGuid().ToString() + ".jpg";
                //string fullPath = savePath + "/" + imgName;
                //fileBase.SaveAs(fullPath);
                #region
                MemoryStream ms = new MemoryStream();
                System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(fileBase.InputStream);
                imgPhoto.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] bytes = ms.GetBuffer();

                string imgName = Guid.NewGuid().ToString() + ".jpg";
                HttpUploadFile fileUp = new HttpUploadFile();
                // fileBase.SaveAs();
                string result = fileUp.UploadFile(Config.fileuploadUrl, "JiaQunTwoCode", imgName, bytes);//b：byte[]类型（上传文件的文件流）
                var obj = JsonTools.ToObject<uploadEntity>(result);
                #endregion
                WeiQunTwoCodeBLL.Insert(name, obj.url);
                ViewBag.Result = true;
            }
            catch (Exception)
            {
                ViewBag.Result = false;
            }

            return View();

        }


    }
}
